﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PSI.Helper;
using PSI.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Web;

namespace PSI.Services {
    public class BillNumberService : PSIBaseService {
        /// <summary>
        /// 生成单据自动编号
        /// </summary>
        /// <param name="billId"></param>
        /// <returns></returns>
        public string GetAutoNumber(string billId, string billPrefix, string billMid, int suffixLength = 3) {
            var result = string.Empty;
            if (billPrefix == null) {
                billPrefix = string.Empty;
            }

            // 首先查询有没有退回的单号可用
            var sql = "select top 1 full_num from psi_bill_auto_number where num_flag <> 0 and bill_id = @billId order by full_num";
            var p = CreateDbParameters();
            p.AddWithValue("billId", billId);

            Query(sql, p, reader => {
                result = reader.GetString(0);
            });

            if (!string.IsNullOrEmpty(result)) {
                return result;
            }

            sql = @"select num_prefix, num_mid, num_suffix 
                    from psi_bill_auto_number 
                    where num_flag = 0 and bill_id = @billId 
                        and num_prefix = @prefix and num_mid = @mid and len(num_suffix) = @suffixLength";
            var prefix = string.Empty;
            var mid = string.Empty;
            var suffix = string.Empty;
            p = CreateDbParameters();
            p.AddWithValue("billId", billId);
            p.AddWithValue("prefix", billPrefix);
            p.AddWithValue("mid", billMid);
            p.AddWithValue("suffixLength", suffixLength);
            Query(sql, p, reader => {
                prefix = reader.GetString(0);
                mid = reader.GetString(1);
                suffix = reader.GetString(2);
            });
            if (string.IsNullOrEmpty(prefix) && string.IsNullOrEmpty(mid) && string.IsNullOrEmpty(suffix)) {
                // 首次生成单号
                var sb = new StringBuilder();
                sb.Append(billPrefix).Append(billMid);
                for (int i = 0; i < suffixLength - 1; i++) {
                    sb.Append("0");
                }
                sb.Append("1");

                result = sb.ToString();

                sb = new StringBuilder();
                for (int i = 0; i < suffixLength - 1; i++) {
                    sb.Append("0");
                }
                sb.Append("2");
                prefix = billPrefix;
                mid = billMid;
                suffix = sb.ToString();

                sql = @"insert into psi_bill_auto_number (bill_id, num_prefix, num_mid, num_suffix, full_num, num_flag)
                        values (@billId, @prefix, @mid, @suffix, @fullNum, 0)";
                p = CreateDbParameters();
                p.AddWithValue("billId", billId);
                p.AddWithValue("prefix", prefix);
                p.AddWithValue("mid", mid);
                p.AddWithValue("suffix", suffix);
                p.AddWithValue("fullNum", prefix + mid + suffix);
                Execute(sql, p);
            } else {
                result = prefix + mid + suffix;

                // 生成下一个可用单号
                var next = (Convert.ToInt32(suffix) + 1).ToString();
                var sb = new StringBuilder();
                for (int i = 0; i < suffixLength - next.Length; i++) {
                    sb.Append("0");
                }
                sb.Append(next);
                suffix = sb.ToString();

                sql = @"update psi_bill_auto_number set num_suffix = @suffix, full_num = @fullNum
                        where bill_id = @billId and num_flag = 0";
                p = CreateDbParameters();
                p.AddWithValue("suffix", suffix);
                p.AddWithValue("fullNum", prefix + mid + suffix);
                p.AddWithValue("billId", billId);
                Execute(sql, p);
            }

            return result;
        }
    }
}